package com.example.admin19.myandfixtext;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;

import com.example.admin19.myandfixtext.Bean.PathBean;
import com.example.admin19.myandfixtext.Utils.GsonUtils;
import com.example.admin19.myandfixtext.Utils.RepairBugUtil;
import com.example.admin19.myandfixtext.Utils.WeakHandler;
import com.umeng.onlineconfig.OnlineConfigAgent;

public class MainActivity extends Activity {

    public static final int MSG_WHAT_DOWNLOAD = 0x111;
    public static final String UMENG_ONLINE_PARAM="pathInfo";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerMessageReceiver();
        initUmeng();// 初始化友盟在线参数
        getUmengParamAndFix();
        //  dadadadada

        ///xaxaxaxax
    }

    private void initUmeng() {
        OnlineConfigAgent.getInstance().updateOnlineConfig(this);
        OnlineConfigAgent.getInstance().setDebugMode(true);
    }
    public void onChick(View view){
            TestActivity.launch(this);
    }


    private MessageReceiver mMessageReceiver;

    public static final String MESSAGE_RECEIVED_ACTION = "com.example.admin19.myandfixtext.MESSAGE_RECEIVED_ACTION";
    public static final String KEY_MESSAGE = "message";


    public void registerMessageReceiver(){
        mMessageReceiver=new MessageReceiver();
        IntentFilter filter=new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(MESSAGE_RECEIVED_ACTION);
        registerReceiver(mMessageReceiver,filter);
        getUmengParamAndFix();
    }

    private void getUmengParamAndFix() {
        String pathInfo=OnlineConfigAgent.getInstance().getConfigParams(this,UMENG_ONLINE_PARAM);
        if(TextUtils.isEmpty(pathInfo)){
            PathBean bean= GsonUtils.getInstance().parseIfNull(PathBean.class,pathInfo);
            try {
                //判断当前的版本是否有补丁需要下载更新
                RepairBugUtil.getInstance().comparePath(this,bean);
            } catch (Exception e) {
                e.printStackTrace();
            }


        }
    }
    private WeakHandler mHandler=new WeakHandler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            if (message.what==MSG_WHAT_DOWNLOAD){
                String meaage= (String) message.obj;
                if (TextUtils.isEmpty(meaage)){
                    return false;
                }
                try {
                    PathBean pathbean=GsonUtils.getInstance().parse(PathBean.class,meaage);
                    RepairBugUtil.getInstance().comparePath(MainActivity.this,pathbean);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return false;
        }
    });


    public class MessageReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())){
                String message=intent.getStringExtra(KEY_MESSAGE);
                Message  msg=new Message();
                msg.what=MSG_WHAT_DOWNLOAD;
                msg.obj=message;
                mHandler.sendMessage(msg);
            }

        }
    }
}
